import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/Layout'

import systemRouter from './modules/system'
import componentRouter from './modules/component'
import testRouter from './modules/testRouter'

Vue.use(VueRouter)

export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },

  {
    path: '/login',
    component: () => import('views/login/index'),
    name: 'Login',
    meta: { title: '登录' },
    hidden: true
  },

  // {
  //   path: '/dashboard',
  //   component: () => import('views/dashboard/index'),
  //   name: 'Dashboard',
  //   meta: { title: 'Dashboard', icon: '' },
  // },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    meta: { flag: 1 },
    children: [
      {
        path: '/dashboard',
        component: () => import('views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '首页', icon: 'home', iconType: 'mdi' }
      }
    ]
  }
]

// const modulesFiles = require.context('./modules', true, /\.js$/)

// const modules = modulesFiles.keys().reduce((modules, modulePath) => {
//   const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')

//   const value = modulesFiles(modulePath)

//   modules[moduleName] = value.default

//   return modules
// }, {})

// export const asyncRoutes = Object.values(modules)
export const asyncRoutes = [systemRouter, componentRouter, testRouter]

// console.log(asyncRoutes)

const createRouter = () =>
  new VueRouter({
    // mode: 'history',
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  })

const router = createRouter()

// 重置路由
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}

export default router
